/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.gatech.gellinservlet.model;
import java.io.Serializable;
import java.io.*;
import java.util.ArrayList;
import java.util.logging.Level;
/**
 *
 * @author Tyler Powell
 */
public class TeamManager extends DataManager<Team> implements Serializable{
    
    public TeamManager()
    {
        super();
    }
    
    /**
     * Creates a new team inside the course.
     * @param name The name of the new team.
     * @param maxSize The maximum size of students the team can hold.
     * @return The newly created team.
     */
    public Team createTeam(String name, int maxSize, int teamDesignation)
    {
        Team team = new Team(name, maxSize, teamDesignation);
        add(team);
        return team;
    }
    
    public void addStudentToTeam(Student stud, int teamID)
    {
        Team aTeam = search(teamID);
        if(aTeam.getTeamDesignation() == Lab.PICKTEAM)
        {
            aTeam.add(stud);
        }
        stud.addTeam(aTeam);   
    }
    
    public void removeStudentFromTeam(Student stud, int teamID)
    {
        Team aTeam = search(teamID);
        aTeam.remove(stud);
        stud.removeTeam(aTeam);
    }
    
    /**
      * Save this object via serialization
      * 
      * @param filename the name of the file to save to
      */
     public void save(String filename) {
         try {
             /*
              * Create the object output stream for serialization.
              * We are wrapping a FileOutputStream since we
              * want to save to disk.  You can also save to socket
              * streams or any other kind of stream.
              */
            ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(filename));
            
            /*
             * The only real call we need.  The stream buffers the output and reuses
             * data, so if you are serializing very frequently, then the object values might
             * not actually change because the old serialized object is being reused.
             * 
             * To fix this you can try writeUnshared() or you can reset the stream.
             * out.reset();
             */
            out.writeObject(this.getDataList());
            //out.writeObject(this.getDataList()); //this writes out the members, so we know that works
        } catch (FileNotFoundException e) {
            Log.getInstance().log(Level.SEVERE, "Save file not found: " + filename, e);
        } catch (IOException e) {
            Log.getInstance().log(Level.SEVERE, "General IO Error on saving: " + filename, e);
        }
         
     }
     
     /**
     * This is an example of a factory method
     * We call this static method to create an
     * instance of UserManager from a serialized file.
     *
     * @param filename the name of the file to use
     * @return an instance of UserManager initialized from a file.
     */
     public void getFromFile(String filename) {
         ArrayList c = null;
         try {
             /*
              * Create the input stream.  Since we want to read from the disk,
              * we wrap a file stream.
              */
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(filename));
            /*
             * Now we can read the entire company in with only one call
             */
            c = (ArrayList) in.readObject();
            this.setDataList(c);

        } catch (FileNotFoundException e) {
            Log.getInstance().log(Level.SEVERE, "Load file not found: " + filename, e);
        } catch (IOException e) {
            Log.getInstance().log(Level.SEVERE, "General IO Error on loading: " + filename, e);
        } catch (ClassNotFoundException e) {
            Log.getInstance().log(Level.SEVERE, "Company class not found on loading: " + filename, e);
        }
        
     }
}
